package 力扣日常刷题.木22二月.第20天226;

/**
 * @author 帅小伙
 * @date 2022/2/26
 * @description
 */
public class Demo165增量元素之间的最大差值 {

    public int maximumDifference(int[] nums) {
        int n = nums.length;
        if (n == 1) return 0;

        int ans = 0, min = nums[0], max = nums[0];
        for (int i = 0; i < nums.length; i++) {
            if (min > nums[i]) {
                min = nums[i];
                max = nums[i];
            }
            if (max < nums[i]) max = nums[i];
            ans = Math.max(ans, max == min ? -1 : max - min);
        }
        return ans;
    }

}
